<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">亚朵酒店后台登录</h3>
      </div>

      <el-form-item prop="username" style="width:80% ">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="Username"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password"  style="width:80%">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="password"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>

      <el-button
        :loading="loading"
        type="primary"
        style="width:80%;margin-bottom:30px;"
        @click.native.prevent="handleLogin"
        class="a"
      >登录</el-button>

      <div class="tips">
        <span style="margin-right:20px;">username: admin</span>
        <span> password: 00000</span>
      </div>
    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { mapActions } from 'vuex'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('用户名不正确'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: '111111'
      },
      loginRules: {
        username: [
          {
            required: true,
            trigger: 'blur',
            message: '用户名不能为空',
            validator: validateUsername
          }
        ],
        password: [
          {
            required: true,
            trigger: 'blur',
            validator: validatePassword,
            min: 6,
            max: 16,
            message: '密码的长度在6-16位之间 '
          }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    ...mapActions(['user/login']),
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true

          // 验证账号密码
          localStorage.setItem('token',(Math.random()*10000).toString())
          this.$router.push("/")
          // this.$store
          //   .dispatch('user/login', this.loginForm)
          //   .then(() => {
          //     this.$router.push({ path: this.redirect || '/' })
          //     this.loading = false
          //   })
          //   .catch(() => {
          //     this.loading = false
          //   })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">

.a {
    position: relative;
    padding: 12px 40px;
    border-radius: 30px;
    font-size: 30px;
    color: #FFF;
    text-decoration: none;
    /* 使用linear-gradient生成一个线性渐变的“背景图” */
    background-image: linear-gradient(90deg, #03a9f4, #f441a5, #ccc68c, #03a9f4);
    /* 放大背景图 */
    background-size: 400%;
    /* 这里是为了让a标签的背景显示在a::before的下面 */
    z-index: 1;
}

.a:hover {
    /* 创建动画
        动画名称show
        执行时间8秒
        线性
        循环执行
    */
    animation: show 8s linear infinite;
}

/* 动画主体
    从初始到结束，移动背景图的位置
*/

@keyframes show {
    0% {
        background-position: 0%;
    }
    100% {
        background-position: 400%;
    }
}

/* 创建背景炫光效果 */
.a::before{
    position: absolute;
    content: "";
    top:-5px;
    left:-5px;
    right:-5px;
    bottom:-5px;
    /* 使用linear-gradient生成一个线性渐变的“背景图” */
    background-image: linear-gradient(90deg, #03a9f4, #f441a5, #ffeb3b, #03a9f4);
    /* 放大背景图 */
    background-size: 400%;
    border-radius: 40px;
    z-index: -1;
    filter: blur(20px);
    /* 默认不显示 */
    opacity: 0;
}
.a:hover::before{
    opacity: 1;
    /* 背景炫光增加动画 */
    /* 与按钮的动画保持一致 */
    /* 创建动画
        动画名称show
        执行时间8秒
        线性
        循环执行
    */
    animation: show 8s linear infinite;
}

/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
// $light_gray: #fff;
$light_gray: #68b0fe; // 将输入框颜色改成蓝色
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  // 如需要在样式表中使用@别名的时候，需要在@前面加上一个~符号，否则就不会识别
  // background-image 属性会在元素的背景中设置一个图像。
  // background-position 属性设置背景图像的起始位置。
  background: url('~@/assets/common/12.jpg') center;
  background-size: cover;

  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  // .el-form-item {
  //   border: 1px solid rgba(255, 255, 255, 0.1);
  //   background: rgba(0, 0, 0, 0.1);
  //   border-radius: 5px;
  //   color: #454545;
  // }
  .el-form-item {
    // border: 1px solid rgba(255, 255, 255, 0.1);
    border: 3px solid #cec8c8;
    // background: rgba(248, 210, 210, 1); // 输入登录表单的背景色
    border-radius: 5px;
    color: #615f5f;
    
  }
  .el-form-item__content {
    height: 40px;
    line-height: 30px;
  }
 
}
.login-container .el-input input {
    height: 43px;
  }
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    background-color: white;
    border-radius: 5%;
    position: absolute;
    bottom: 35%;
    left:5% ;
    width: 520px;
    max-width: 100%;
    /* padding: 160px 35px 0;
    margin: 0 auto; */
    padding: 58px 0px 0 88px;
    // padding-left: 35px;
  
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    right: 9%;

    .title {
      font-size: 40px;
      // color: $light_gray;
      color: rgb(127, 188, 245);
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 12px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
